//
//  View+Alert.swift
//  NyamanDana
//
//  Created by 李永彬 on 2025/10/9.
//

import SwiftUI

// View 扩展 - 支持 dismiss 回调
extension View {
    
    // MARK: - Window 弹窗（完全自定义内容）
    func ndCustomPopupOnWindow<Content: View>(
        isPresented: Binding<Bool>,
        style: NDPopupStyle = .alert,
        backgroundImage: String? = nil,
        onDismissCompleted: (() -> Void)? = nil, // 新增参数
        @ViewBuilder content: @escaping () -> Content
    ) -> some View {
        let config = NDPopupConfig(
            style: style,
            backgroundImage: backgroundImage,
            animation: .spring(response: 0.3, dampingFraction: 0.8),
            padding: 16
        )
        
        return self.onChange(of: isPresented.wrappedValue) { newValue in
            print("弹窗状态变化: \(newValue)")
            if newValue {
                NDWindowPopupManager.shared.showPopup(
                    isPresented: isPresented,
                    config: config,
                    onDismissCompleted: onDismissCompleted, // 传递回调
                    content: content
                )
            } else {
                NDWindowPopupManager.shared.dismissPopup(for: isPresented)
            }
        }
    }
    
    // MARK: - Window 标准弹窗
    func ndAlertOnWindow(
        isPresented: Binding<Bool>,
        title: String,
        tips: String?,
        message: String? = nil,
        textAlignment: TextAlignment? = .center,
        backgroundImage: String? = nil,
        onDismissCompleted: (() -> Void)? = nil, // 新增参数
        buttons: [NDPopupButton]
    ) -> some View {
        let standardView = NDPopupStandardView(
            title: title,
            tips: tips,
            message: message,
            backgroundImage: backgroundImage,
            textAlignment: textAlignment,
            buttons: buttons
        )
        
        return self.ndCustomPopupOnWindow(
            isPresented: isPresented,
            style: .alert,
            backgroundImage: backgroundImage,
            onDismissCompleted: onDismissCompleted // 传递回调
        ) {
            standardView
        }
    }
    
    // MARK: - Window ActionSheet
    func ndActionSheetOnWindow(
        isPresented: Binding<Bool>,
        title: String? = nil,
        tips: String?,
        message: String? = nil,
        backgroundImage: String? = nil,
        onDismissCompleted: (() -> Void)? = nil, // 新增参数
        buttons: [NDPopupButton]
    ) -> some View {
        let standardView = NDPopupStandardView(
            title: title,
            tips: tips,
            message: message,
            backgroundImage: backgroundImage,
            buttons: buttons
        )
        
        return self.ndCustomPopupOnWindow(
            isPresented: isPresented,
            style: .actionSheet,
            backgroundImage: backgroundImage,
            onDismissCompleted: onDismissCompleted // 传递回调
        ) {
            standardView
        }
    }
    
    func ndCancelAccountAlertOnWindow(
        isPresented: Binding<Bool>,
        isSelected: Binding<Bool>, // 添加 Binding 参数
        title: String,
        tips: String?,
        message: String? = nil,
        textAlignment: TextAlignment? = .center,
        backgroundImage: String? = nil,
        onDismissCompleted: (() -> Void)? = nil,
        buttons: [NDPopupButton],
        onClickPravicy: (() -> Void)? = nil // 添加回调参数
    ) -> some View {
        let standardView = NDCancelAccountAlertView(
            title: title,
            tips: tips,
            message: message,
            backgroundImage: backgroundImage,
            isSelected: isSelected, // 传递 Binding
            textAlignment: textAlignment,
            buttons: buttons,
            onClickPravicy: onClickPravicy // 传递回调
        )
        
        return self.ndCustomPopupOnWindow(
            isPresented: isPresented,
            style: .alert,
            backgroundImage: backgroundImage,
            onDismissCompleted: onDismissCompleted // 传递回调
        ) {
            standardView
        }
    }
    
    func ndCancelAccountAlert(
        isPresented: Binding<Bool>,
        isSelected: Binding<Bool>,
        title: String,
        tips: String?,
        message: String? = nil,
        textAlignment: TextAlignment? = .center,
        backgroundImage: String? = nil,
        onDismissCompleted: (() -> Void)? = nil,
        buttons: [NDPopupButton],
        onClickPravicy: (() -> Void)? = nil
    ) -> some View {
        self.overlay(
            Group {
                if isPresented.wrappedValue {
                    NDCancelAccountAlertView(
                        title: title,
                        tips: tips,
                        message: message,
                        backgroundImage: backgroundImage,
                        isSelected: isSelected,
                        textAlignment: textAlignment,
                        buttons: buttons,
                        onClickPravicy: onClickPravicy
                    )
                    .transition(.opacity)
                    .zIndex(999) // 确保在最上层
                }
            }
        )
    }
}
